#include <iostream>

#include "Connection.h"
#include "ConnectionPoll.h"

using namespace std;

int main()
{
    // Connection conn;
    // char sql[1024] = {0};
    // // sprintf()生成的是string
    // sprintf(sql, "insert into user(name, age, sex) values('%s', '%d', '%s')", "zhang san", 20, "male");

    // conn.connect("127.0.0.1", 3306, "root", "123789", "chat");
    // conn.update(sql);

    // // 获取连接池类指针
    // ConnectionPool * connPool = ConnectionPool::getConnectionPool();
    // // 加载连接池配置
    // connPool->init();

    // std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
    // 模拟一千个人每人连接一次，而不是一个人一千次，注意
    // for (int i = 0; i < 1000; ++i)
    // {
    // // 1.不使用连接池
    // Connection conn;
    // char sql[1024] = {0};
    // sprintf(sql, "insert into user(name, age, sex) values('%s', '%d', '%s')", "zhang san", 20, "male");
    // conn.connect("127.0.0.1", 3306, "root", "123789", "chat");
    // conn.update(sql);
    // }

    std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
    // 2.使用连接池
    ConnectionPool *connpool = ConnectionPool::getConnectionPool(); // 获取连接池对象
    for (int i = 0; i < 1000; ++i)
    {
        std::shared_ptr<Connection> connptr = connpool->getIdleConn(); // 得到一个空闲连接给智能指针对象
        char sql[1024] = {0};
        sprintf(sql, "insert into user(name, age, sex) values('%s', '%d', '%s')", "zhang san", 20, "male");
        connptr->update(sql);
    }
    std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
    int64_t duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
    printf("Time spent: %ld ms\n", duration);

    return 0;
}